package www.dianbaozu.com.ui.manage

import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.netease.nim.uikit.xpopup.CreateXPopupHelper
import com.netease.nim.uikit.xpopup.PopupViewEntity
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentGroupSettingBinding
import www.dianbaozu.com.extension.requestUniversally
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.GroupSettingEntity
import www.wen.com.common.lib.data.entities.RequestBodyParameter
import www.wen.com.common.lib.extension.getString
import www.wen.com.common.lib.extension.postObject
import www.wen.com.common.lib.extension.toast
import www.wen.com.common.lib.extension.trimProxy
import www.wen.com.common.lib.helper.loading.KeyHelper
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter


/**
 * @author WEN
 * @Description:分组设置
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class GroupSettingFragment : BaseLoadingFragment<FragmentGroupSettingBinding>(){

    lateinit var mGroupSettingEntity: GroupSettingEntity

    val mBaseApiViewModel: BaseApiViewModel by viewModels()

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override fun isEnableLoadingHelper() = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
            requireActivity(),
            tilte = getString(R.string.com_grouping_settings),
            isShowRightText = true,
            rightText = "保存",
            action = ::titleAction
    )
    override fun getLayoutId(): Int = R.layout.fragment_group_setting

    override fun actionRight() {
        if(dataBinding.etGroupName.trimProxy().isBlank()){
            toast("分组名称不能为空")
            return
        }
        mBaseApiViewModel.requestUniversally(
                viewModelScope = lifecycleScope,
                tipSuccess = "",
                isShowToastTip = false,
                request = { appDataManage -> appDataManage.requestUpdateGroup(mGroupSettingEntity.groupId, RequestBodyParameter(
                        groupName = dataBinding.etGroupName.text.toString()
                )) }
        ) {
            mGroupSettingEntity.groupName = dataBinding.etGroupName.text.toString()
            //刷新首页-服务分组
            postObject(KeyHelper.REFRESHING_SERVER_LIST_KEY1,Object())
            //刷新频道管理的分组名称
            postObject(KeyHelper.GROUP_RESET_SORT_KEY2, mGroupSettingEntity)
            toast("修改名称成功")
            //返回上一页
            actionLeft()
        }
    }

    override fun lazyInitView() {
        mGroupSettingEntity = requireArguments().getParcelable(BUNDLE_DATA)!!
        mGroupSettingEntity.groupName.also { dataBinding.etGroupName.setText(it) }
    }

    override fun onClick(v: View) {
        if(mGroupSettingEntity.isCanDelete){
            CreateXPopupHelper.createShowStandardAlertPopupView(PopupViewEntity().apply {
                context = requireActivity()
                standardAlertPopupViewTitle = getString(R.string.com_delete_group)
                isShow = true
                standardAlertPopupViewCancelContent = "取消"
                standardAlertPopupViewConfirmContent = "确认"
                standardAlertPopupViewContent = "确定要删除 【${mGroupSettingEntity.groupName}】 吗? 该操作无法撤销！"
                confirmFunction = {
                    mBaseApiViewModel.requestUniversally(
                            viewModelScope = lifecycleScope,
                            tipSuccess = "",
                            isShowToastTip = false,
                            request = { appDataManage -> appDataManage.deleteGroup(mGroupSettingEntity.groupId) }
                    ) {
                        //刷新首页-服务分组
                        postObject(KeyHelper.REFRESHING_SERVER_LIST_KEY1,Object())
                        //刷新频道管理的分组排序
                        postObject(KeyHelper.GROUP_RESET_SORT_KEY1, mGroupSettingEntity.position)
                        toast("删除分组成功")
                        //返回上一页
                        actionLeft()
                    }
                }
            })
        }else{
            toast("请先移除分组中的频道")
        }
    }

}



